<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.2.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/blog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/blog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/blog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/blog/css/main.css">


<link rel="stylesheet" href="/blog/lib/font-awesome/css/font-awesome.min.css">
  <link rel="stylesheet" href="/blog/lib/pace/pace-theme-bounce.min.css">
  <script src="/blog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"ipvb.gitee.io","root":"/blog/","scheme":"Gemini","version":"7.7.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false,"b2t":true,"scrollpercent":true},"copycode":{"enable":true,"show_result":true,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="前言树是数据结构中的重中之重，尤其以各类二叉树为学习的难点。一直以来，对于树的掌握都是模棱两可的状态，现在希望通过写一个关于二叉树的专题系列。在学习与总结的同时更加深入的了解掌握二叉树。本系列文章将着重介绍一般二叉树、完全二叉树、满二叉树、线索二叉树、霍夫曼树、二叉排序树、平衡二叉树、红黑树、B树。">
<meta property="og:type" content="article">
<meta property="og:title" content="数据结构（二叉树）">
<meta property="og:url" content="https://ipvb.gitee.io/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/index.html">
<meta property="og:site_name" content="Chenyumeng的博客">
<meta property="og:description" content="前言树是数据结构中的重中之重，尤其以各类二叉树为学习的难点。一直以来，对于树的掌握都是模棱两可的状态，现在希望通过写一个关于二叉树的专题系列。在学习与总结的同时更加深入的了解掌握二叉树。本系列文章将着重介绍一般二叉树、完全二叉树、满二叉树、线索二叉树、霍夫曼树、二叉排序树、平衡二叉树、红黑树、B树。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/1.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/2.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/3.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/4.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/5.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/6.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/7.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/8.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/9.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/10.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/11.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/12.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/13.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/14.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/15.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/16.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/17.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/18.jpg">
<meta property="og:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/19.jpg">
<meta property="article:published_time" content="2020-04-20T08:38:55.000Z">
<meta property="article:modified_time" content="2020-07-04T11:50:46.657Z">
<meta property="article:author" content="Chenyumeng">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="js">
<meta property="article:tag" content="jquery">
<meta property="article:tag" content="javascript">
<meta property="article:tag" content="html5">
<meta property="article:tag" content="开发者">
<meta property="article:tag" content="程序猿">
<meta property="article:tag" content="程序媛">
<meta property="article:tag" content="极客">
<meta property="article:tag" content="编程">
<meta property="article:tag" content="代码">
<meta property="article:tag" content="开源">
<meta property="article:tag" content="IT网站">
<meta property="article:tag" content="Developer">
<meta property="article:tag" content="Programmer">
<meta property="article:tag" content="Coder">
<meta property="article:tag" content="Geek">
<meta property="article:tag" content="html">
<meta property="article:tag" content="css">
<meta property="article:tag" content="css3">
<meta property="article:tag" content="用户体验">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ipvb.gitee.io/blog/assets/BinaryTree/1.jpg">

<link rel="canonical" href="https://ipvb.gitee.io/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true
  };
</script>

  <title>数据结构（二叉树） | Chenyumeng的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

  <link rel="stylesheet" href="/dist/css/share.min.css">
</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <div>
      <a href="/blog/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Chenyumeng的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">热爱自己，热爱生活</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/blog/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/blog/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/blog/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签<span class="badge">29</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/blog/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类<span class="badge">1</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/blog/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档<span class="badge">32</span></a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>

</nav>
  <div class="site-search">
    <div class="search-pop-overlay">
  <div class="popup search-popup">
      <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocorrect="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

  </div>
</div>

  </div>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

  <a href="https://github.com/chen-yumeng" class="github-corner" title="Follow me on GitHub" aria-label="Follow me on GitHub" rel="noopener" target="_blank"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content">
            

  <div class="posts-expand">
      
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block " lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://ipvb.gitee.io/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/blog/images/touxiang.JPG">
      <meta itemprop="name" content="Chenyumeng">
      <meta itemprop="description" content="用来记录自己学习中所遇到的问题以及如何解决和自己所学知识的理解">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Chenyumeng的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          数据结构（二叉树）
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2020-04-20 16:38:55" itemprop="dateCreated datePublished" datetime="2020-04-20T16:38:55+08:00">2020-04-20</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2020-07-04 19:50:46" itemprop="dateModified" datetime="2020-07-04T19:50:46+08:00">2020-07-04</time>
              </span>

          
            <span id="/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/" class="post-meta-item leancloud_visitors" data-flag-title="数据结构（二叉树）" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span><br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="fa fa-file-word-o"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>4.9k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="fa fa-clock-o"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>4 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h3 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h3><p><strong>树</strong>是数据结构中的重中之重，尤其以各类二叉树为学习的难点。一直以来，对于树的掌握都是模棱两可的状态，现在希望通过写一个关于二叉树的专题系列。在学习与总结的同时更加深入的了解掌握二叉树。本系列文章将着重介绍一般二叉树、完全二叉树、满二叉树、线索二叉树、霍夫曼树、二叉排序树、平衡二叉树、红黑树、B树。</p>
<a id="more"></a>

<h3 id="1-重点概念"><a href="#1-重点概念" class="headerlink" title="1 重点概念"></a>1 重点概念</h3><h4 id="1-1-结点概念"><a href="#1-1-结点概念" class="headerlink" title="1.1 结点概念"></a>1.1 结点概念</h4><p><strong>结点</strong>是数据结构中的基础，是构成复杂数据结构的基本组成单位。</p>
<h4 id="1-2-树结点声明"><a href="#1-2-树结点声明" class="headerlink" title="1.2 树结点声明"></a>1.2 树结点声明</h4><p>本系列文章中提及的结点专指树的结点。例如：结点A在图中表示为：</p>
<p><img src="/blog/assets/BinaryTree/1.jpg" alt="img"></p>
<h3 id="2-树"><a href="#2-树" class="headerlink" title="2 树"></a>2 树</h3><h4 id="2-1-定义"><a href="#2-1-定义" class="headerlink" title="2.1 定义"></a>2.1 定义</h4><p><strong>树（Tree）</strong>是n（n&gt;=0)个结点的有限集。n=0时称为空树。在任意一颗非空树中：<br> 1）有且仅有一个特定的称为根（Root）的结点；<br> 2）当n&gt;1时，其余结点可分为m(m&gt;0)个互不相交的有限集T1、T2、……、Tn，其中每一个集合本身又是一棵树，并且称为根的子树。</p>
<p>此外，树的定义还需要强调以下两点：<br> 1）n&gt;0时根结点是唯一的，不可能存在多个根结点，数据结构中的树只能有一个根结点。<br> 2）m&gt;0时，子树的个数没有限制，但它们一定是互不相交的。<br> 示例树：<br>一棵普通的树：</p>
<p><img src="/blog/assets/BinaryTree/2.jpg" alt="img"></p>
<p>​                                                                                    普通树</p>
<p>由树的定义可以看出，树的定义使用了递归的方式。递归在树的学习过程中起着重要作用，如果对于递归不是十分了解，建议先看看<a href="https://blog.csdn.net/feizaosyuacm/article/details/54919389" target="_blank" rel="noopener">递归算法</a></p>
<h4 id="2-2-结点的度"><a href="#2-2-结点的度" class="headerlink" title="2.2 结点的度"></a>2.2 结点的度</h4><p>结点拥有的子树数目称为结点的<strong>度</strong>。<br> 图2.2中标注了图2.1所示树的各个结点的度。</p>
<p><img src="/blog/assets/BinaryTree/3.jpg" alt="img"></p>
<p>​                                                                                    度示意图</p>
<h5 id="2-3-结点关系"><a href="#2-3-结点关系" class="headerlink" title="2.3 结点关系"></a>2.3 结点关系</h5><p>结点子树的根结点为该结点的<strong>孩子结点</strong>。相应该结点称为孩子结点的<strong>双亲结点</strong>。<br> A为B的双亲结点，B为A的孩子结点。<br> 同一个双亲结点的孩子结点之间互称<strong>兄弟结点</strong>。<br> 结点B与结点C互为兄弟结点。</p>
<h5 id="2-4-结点层次"><a href="#2-4-结点层次" class="headerlink" title="2.4 结点层次"></a>2.4 结点层次</h5><p>从根开始定义起，根为第一层，根的孩子为第二层，以此类推。</p>
<p><img src="/blog/assets/BinaryTree/4.jpg" alt="img"></p>
<p>​                                                                            树的层次关系</p>
<h5 id="2-5-树的深度"><a href="#2-5-树的深度" class="headerlink" title="2.5 树的深度"></a>2.5 树的深度</h5><p>树中结点的最大层次数称为树的深度或高度。上图所示树的深度为4。</p>
<h3 id="3-二叉树"><a href="#3-二叉树" class="headerlink" title="3 二叉树"></a>3 二叉树</h3><h4 id="3-1-定义"><a href="#3-1-定义" class="headerlink" title="3.1 定义"></a>3.1 定义</h4><p><strong>二叉树</strong>是n(n&gt;=0)个结点的有限集合，该集合或者为空集（称为空二叉树），或者由一个根结点和两棵互不相交的、分别称为根结点的左子树和右子树组成。<br>一棵普通二叉树：</p>
<p><img src="/blog/assets/BinaryTree/5.jpg" alt="img"></p>
<p>​                                                                                    二叉树</p>
<h4 id="3-2-二叉树特点"><a href="#3-2-二叉树特点" class="headerlink" title="3.2 二叉树特点"></a>3.2 二叉树特点</h4><p>由二叉树定义以及图示分析得出二叉树有以下特点：<br> 1）每个结点最多有两颗子树，所以二叉树中不存在度大于2的结点。<br> 2）左子树和右子树是有顺序的，次序不能任意颠倒。<br> 3）即使树中某结点只有一棵子树，也要区分它是左子树还是右子树。</p>
<h4 id="3-3-二叉树性质"><a href="#3-3-二叉树性质" class="headerlink" title="3.3 二叉树性质"></a>3.3 二叉树性质</h4><p>1）在二叉树的第i层上最多有2i-1 个节点 。（i&gt;=1）<br> 2）二叉树中如果深度为k,那么最多有2k-1个节点。(k&gt;=1）<br> 3）n0=n2+1  n0表示度数为0的节点数，n2表示度数为2的节点数。<br> 4）在完全二叉树中，具有n个节点的完全二叉树的深度为[log2n]+1，其中[log2n]是向下取整。<br> 5）若对含 n 个结点的完全二叉树从上到下且从左至右进行 1 至 n 的编号，则对完全二叉树中任意一个编号为 i 的结点有如下特性：</p>
<blockquote>
<p>(1) 若 i=1，则该结点是二叉树的根，无双亲, 否则，编号为 [i/2] 的结点为其双亲结点;<br> (2) 若 2i&gt;n，则该结点无左孩子，  否则，编号为 2i 的结点为其左孩子结点；<br> (3) 若 2i+1&gt;n，则该结点无右孩子结点，  否则，编号为2i+1 的结点为其右孩子结点。</p>
</blockquote>
<h4 id="3-4-斜树"><a href="#3-4-斜树" class="headerlink" title="3.4 斜树"></a>3.4 斜树</h4><p><strong>斜树</strong>：所有的结点都只有左子树的二叉树叫左斜树。所有结点都是只有右子树的二叉树叫右斜树。这两者统称为斜树。</p>
<p><img src="/blog/assets/BinaryTree/6.jpg" alt="img"></p>
<p>​                                                                                    左斜树</p>
<p><img src="/blog/assets/BinaryTree/7.jpg" alt="img"></p>
<p>​                                                                                    右斜树</p>
<h4 id="3-5-满二叉树"><a href="#3-5-满二叉树" class="headerlink" title="3.5 满二叉树"></a>3.5 满二叉树</h4><p><strong>满二叉树</strong>：在一棵二叉树中。如果所有分支结点都存在左子树和右子树，并且所有叶子都在同一层上，这样的二叉树称为满二叉树。<br> 满二叉树的特点有：<br> 1）叶子只能出现在最下一层。出现在其它层就不可能达成平衡。<br> 2）非叶子结点的度一定是2。<br> 3）在同样深度的二叉树中，满二叉树的结点个数最多，叶子数最多。</p>
<p><img src="/blog/assets/BinaryTree/8.jpg" alt="img"></p>
<p>​                                                                                        满二叉树</p>
<h4 id="3-6-完全二叉树"><a href="#3-6-完全二叉树" class="headerlink" title="3.6 完全二叉树"></a>3.6 完全二叉树</h4><p><strong>完全二叉树</strong>：对一颗具有n个结点的二叉树按层编号，如果编号为i(1&lt;=i&lt;=n)的结点与同样深度的满二叉树中编号为i的结点在二叉树中位置完全相同，则这棵二叉树称为完全二叉树。<br> 图3.5展示一棵完全二叉树</p>
<p><img src="/blog/assets/BinaryTree/9.jpg" alt="img"></p>
<p>​                                                                                        完全二叉树</p>
<p><strong>特点</strong></p>
<p>1）叶子结点只能出现在最下层和次下层。<br>2）最下层的叶子结点集中在树的左部。<br>3）倒数第二层若存在叶子结点，一定在右部连续位置。<br>4）如果结点度为1，则该结点只有左孩子，即没有右子树。<br>5）同样结点数目的二叉树，完全二叉树深度最小。</p>
<p><strong>注</strong>:满二叉树一定是完全二叉树，但反过来不一定成立。</p>
<h4 id="3-7-二叉树的存储结构"><a href="#3-7-二叉树的存储结构" class="headerlink" title="3.7 二叉树的存储结构"></a>3.7 二叉树的存储结构</h4><h5 id="3-7-1-顺序存储"><a href="#3-7-1-顺序存储" class="headerlink" title="3.7.1 顺序存储"></a>3.7.1 顺序存储</h5><p>二叉树的顺序存储结构就是使用一维数组存储二叉树中的结点，并且结点的存储位置，就是数组的下标索引。</p>
<p><img src="/blog/assets/BinaryTree/10.jpg" alt="img"></p>
<p>上图所示的一棵完全二叉树采用顺序存储方式，下图表示：</p>
<p><img src="/blog/assets/BinaryTree/11.jpg" alt="img"></p>
<p>​                                                                                            顺序存储</p>
<p>由上图可以看出，当二叉树为完全二叉树时，结点数刚好填满数组。<br> 那么当二叉树不为完全二叉树时，采用顺序存储形式如何呢？例如：对于下图描述的二叉树：</p>
<p><img src="/blog/assets/BinaryTree/12.jpg" alt="img"></p>
<p> 其中浅色结点表示结点不存在。那么图3.8所示的二叉树的顺序存储结构如图3.9所示：</p>
<p><img src="/blog/assets/BinaryTree/13.jpg" alt="img"></p>
<p>其中，∧表示数组中此位置没有存储结点。此时可以发现，顺序存储结构中已经出现了空间浪费的情况。<br> 那么对于右斜树极端情况对应的顺序存储结构如下图所示：</p>
<p><img src="/blog/assets/BinaryTree/14.jpg" alt="img"></p>
<p>由上图可以看出，对于这种右斜树极端情况，采用顺序存储的方式是十分浪费空间的。因此，顺序存储一般适用于完全二叉树。</p>
<h5 id="3-7-2-二叉链表"><a href="#3-7-2-二叉链表" class="headerlink" title="3.7.2 二叉链表"></a>3.7.2 二叉链表</h5><p>既然顺序存储不能满足二叉树的存储需求，那么考虑采用链式存储。由二叉树定义可知，二叉树的每个结点最多有两个孩子。因此，可以将结点数据结构定义为一个数据和两个指针域。表示方式如图3.11所示：</p>
<p><img src="/blog/assets/BinaryTree/15.jpg" alt="img"></p>
<p>定义结点代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Data</span></span><br><span class="line"><span class="meta">@ToString</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">HeroNode</span> </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Object data;</span><br><span class="line">    <span class="keyword">private</span> HeroNode left;</span><br><span class="line">    <span class="keyword">private</span> HeroNode right;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">HeroNode</span><span class="params">(Object data)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">this</span>.data = data;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>则图3.6所示的二叉树可以采用图3.12表示。</p>
<p><img src="/blog/assets/BinaryTree/16.jpg" alt="img"></p>
<p>上图中采用一种链表结构存储二叉树，这种链表称为二叉链表。</p>
<h4 id="3-8-二叉树遍历"><a href="#3-8-二叉树遍历" class="headerlink" title="3.8 二叉树遍历"></a>3.8 二叉树遍历</h4><p>二叉树的遍历一个重点考查的知识点。</p>
<h5 id="3-8-1-定义"><a href="#3-8-1-定义" class="headerlink" title="3.8.1 定义"></a>3.8.1 定义</h5><p><strong>二叉树的遍历</strong>是指从二叉树的根结点出发，按照某种次序依次访问二叉树中的所有结点，使得每个结点被访问一次，且仅被访问一次。<br> 二叉树的访问次序可以分为四种：</p>
<blockquote>
<p>前序遍历<br> 中序遍历<br> 后序遍历<br> 层序遍历</p>
</blockquote>
<h5 id="3-8-2-前序遍历"><a href="#3-8-2-前序遍历" class="headerlink" title="3.8.2 前序遍历"></a>3.8.2 前序遍历</h5><p><strong>前序遍历</strong>通俗的说就是从二叉树的根结点出发，当第一次到达结点时就输出结点数据，按照先向左在向右的方向访问。</p>
<p><img src="/blog/assets/BinaryTree/17.jpg" alt="img"><br> 上图所示二叉树访问如下：</p>
<blockquote>
<p>从根结点出发，则第一次到达结点A，故输出A;<br> 继续向左访问，第一次访问结点B，故输出B；<br> 按照同样规则，输出D，输出H；<br> 当到达叶子结点H，返回到D，此时已经是第二次到达D，故不在输出D，进而向D右子树访问，D右子树不为空，则访问至I，第一次到达I，则输出I；<br> I为叶子结点，则返回到D，D左右子树已经访问完毕，则返回到B，进而到B右子树，第一次到达E，故输出E；<br> 向E左子树，故输出J；<br> 按照同样的访问规则，继续输出C、F、G；</p>
</blockquote>
<p>上图所示二叉树的前序遍历输出为：<br> <strong>ABDHIEJCFG</strong></p>
<h5 id="3-8-3-中序遍历"><a href="#3-8-3-中序遍历" class="headerlink" title="3.8.3 中序遍历"></a>3.8.3 中序遍历</h5><p><strong>中序遍历</strong>就是从二叉树的根结点出发，当第二次到达结点时就输出结点数据，按照先向左在向右的方向访问。</p>
<p>则上图所示二叉树中序访问如下：</p>
<blockquote>
<p>从根结点出发，则第一次到达结点A，不输出A，继续向左访问，第一次访问结点B，不输出B；继续到达D，H；<br> 到达H，H左子树为空，则返回到H，此时第二次访问H，故输出H；<br> H右子树为空，则返回至D，此时第二次到达D，故输出D；<br> 由D返回至B，第二次到达B，故输出B；<br> 按照同样规则继续访问，输出J、E、A、F、C、G；</p>
</blockquote>
<p>则上图所示二叉树的中序遍历输出为：<br> <strong>HDIBJEAFCG</strong></p>
<h5 id="3-8-4-后序遍历"><a href="#3-8-4-后序遍历" class="headerlink" title="3.8.4 后序遍历"></a>3.8.4 后序遍历</h5><p><strong>后序遍历</strong>就是从二叉树的根结点出发，当第三次到达结点时就输出结点数据，按照先向左在向右的方向访问。</p>
<p>则上图所示二叉树后序访问如下：</p>
<blockquote>
<p>从根结点出发，则第一次到达结点A，不输出A，继续向左访问，第一次访问结点B，不输出B；继续到达D，H；<br> 到达H，H左子树为空，则返回到H，此时第二次访问H，不输出H；<br> H右子树为空，则返回至H，此时第三次到达H，故输出H；<br> 由H返回至D，第二次到达D，不输出D；<br> 继续访问至I，I左右子树均为空，故第三次访问I时，输出I；<br> 返回至D，此时第三次到达D，故输出D；<br> 按照同样规则继续访问，输出J、E、B、F、G、C，A；</p>
</blockquote>
<p>则上图所示二叉树的后序遍历输出为：<br> <strong>HIDJEBFGCA</strong><br> 虽然二叉树的遍历过程看似繁琐，但是由于二叉树是一种递归定义的结构，故采用递归方式遍历二叉树的代码十分简单。<br> 递归实现代码如下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 二叉树的前序遍历递归算法</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@param</span> root</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">preOrder</span><span class="params">(HeroNode root)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(root == <span class="keyword">null</span>) &#123;</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    <span class="comment">// 显示结点数据，可以更改为其他对结点操作</span></span><br><span class="line">    System.out.println(root.getData());</span><br><span class="line">    <span class="comment">// 再先序遍历左子树</span></span><br><span class="line">    preOrder(root.getLeft());</span><br><span class="line">    <span class="comment">// 最后先序遍历右子树</span></span><br><span class="line">    preOrder(root.getRight());</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">  * 二叉树的中序遍历递归算法</span></span><br><span class="line"><span class="comment">  * <span class="doctag">@param</span> root</span></span><br><span class="line"><span class="comment">  */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inOrder</span><span class="params">(HeroNode root)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(root == <span class="keyword">null</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 先序遍历左子树</span></span><br><span class="line">    inOrder(root.getLeft());</span><br><span class="line">    <span class="comment">// 再显示结点数据，可以更改为其他对结点操作</span></span><br><span class="line">    System.out.println(root.getData());</span><br><span class="line">    <span class="comment">// 最后先序遍历右子树</span></span><br><span class="line">    inOrder(root.getRight());</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment">     * 二叉树的中序遍历递归算法</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> root</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">inOrder</span><span class="params">(HeroNode root)</span></span>&#123;</span><br><span class="line">    <span class="keyword">if</span>(root == <span class="keyword">null</span>)&#123;</span><br><span class="line">        <span class="keyword">return</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="comment">// 先序遍历左子树</span></span><br><span class="line">    inOrder(root.getLeft());</span><br><span class="line">    <span class="comment">// 再显示结点数据，可以更改为其他对结点操作</span></span><br><span class="line">    System.out.println(root.getData());</span><br><span class="line">    <span class="comment">// 最后先序遍历右子树</span></span><br><span class="line">    inOrder(root.getRight());</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="3-8-5-层次遍历"><a href="#3-8-5-层次遍历" class="headerlink" title="3.8.5 层次遍历"></a>3.8.5 层次遍历</h5><p>层次遍历就是按照树的层次自上而下的遍历二叉树。针对图3.13所示二叉树的层次遍历结果为：<br> <strong>ABCDEFGHIJ</strong><br> 层次遍历的详细方法可以参考<a href="https://blog.csdn.net/lingchen2348/article/details/52774535" target="_blank" rel="noopener">二叉树的按层遍历法</a>。</p>
<h5 id="3-8-6-遍历常考考点"><a href="#3-8-6-遍历常考考点" class="headerlink" title="3.8.6 遍历常考考点"></a>3.8.6 遍历常考考点</h5><p>对于二叉树的遍历有一类典型题型。<br> 1）已知前序遍历序列和中序遍历序列，确定一棵二叉树。<br> 例题：若一棵二叉树的前序遍历为ABCDEF，中序遍历为CBAEDF，请画出这棵二叉树。<br> 分析：前序遍历第一个输出结点为根结点，故A为根结点。早中序遍历中根结点处于左右子树结点中间，故结点A的左子树中结点有CB，右子树中结点有EDF。</p>
<p><img src="/blog/assets/BinaryTree/18.jpg" alt="img"></p>
<p>按照同样的分析方法，对A的左右子树进行划分，最后得出二叉树的形态如图3.15所示：</p>
<p><img src="/blog/assets/BinaryTree/19.jpg" alt="img"></p>
<p>2）已知后序遍历序列和中序遍历序列，确定一棵二叉树。<br> 后序遍历中最后访问的为根结点，因此可以按照上述同样的方法，找到根结点后分成两棵子树，进而继续找到子树的根结点，一步步确定二叉树的形态。<br> <strong>注</strong>：已知前序遍历序列和后序遍历序列，不可以唯一确定一棵二叉树。</p>
<h3 id="4-结语"><a href="#4-结语" class="headerlink" title="4 结语"></a>4 结语</h3><p>通过上述的介绍，已经对于二叉树有了初步的认识。本篇文章介绍的基础知识希望读者能够牢牢掌握，并且能够在脑海中建立一棵二叉树的模型，为后续学习打好基础。</p>
<h3 id="大部分引用"><a href="#大部分引用" class="headerlink" title="大部分引用"></a>大部分引用</h3><p>作者：MrHorse1992<br>链接：<a href="https://www.jianshu.com/p/bf73c8d50dc2" target="_blank" rel="noopener">https://www.jianshu.com/p/bf73c8d50dc2</a><br>来源：简书著作权归作者所有。</p>

    </div>

    
    
    

    
    <div>    
      
        <ul class="post-copyright">
          <li class="post-copyright-link">
          <strong>本文作者：</strong>
          <a href="/" title="欢迎访问 Chenyumeng 的个人博客">Chenyumeng</a>
          </li>

          <li class="post-copyright-link">
            <strong>本文标题：</strong>
            <a href="https://ipvb.gitee.io/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/" title="数据结构（二叉树）">数据结构（二叉树）</a>
          </li>

          <li class="post-copyright-link">
            <strong>本文链接：</strong>
            <a href="https://ipvb.gitee.io/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/" title="数据结构（二叉树）">https://ipvb.gitee.io/blog/2020/04/20/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84%EF%BC%88%E4%BA%8C%E5%8F%89%E6%A0%91%EF%BC%89/</a>
          </li>

          <li class="post-copyright-date">
            <strong>发布时间： </strong>2020年4月20日 - 16时04分
          </li>  

          <li class="post-copyright-license">
            <strong>版权声明： </strong>
            本文由 Chenyumeng 原创。
          </li>
        </ul>
      
    </div>
      

        <div class="reward-container">
  <div></div>
  <button onclick="var qr = document.getElementById('qr'); qr.style.display = (qr.style.display === 'none') ? 'block' : 'none';">
    打赏
  </button>
  <div id="qr" style="display: none;">
      
      <div style="display: inline-block;">
        <img src="/blog/images/wechatpay.png" alt="Chenyumeng 微信支付">
        <p>微信支付</p>
      </div>
      
      <div style="display: inline-block;">
        <img src="/blog/images/alipay.png" alt="Chenyumeng 支付宝">
        <p>支付宝</p>
      </div>

  </div>
</div>


      <div>
        
          <div>
    
        <div style="text-align:center;color: #ccc;font-size:14px;">-------------　　　　本文结束　<i class="fa fa-heart"></i>　感谢您的阅读　　　　-------------</div>
    
</div>
        
      </div>

      <footer class="post-footer">

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/blog/2020/04/20/2020%E5%B9%B404%E6%9C%8820%E6%97%A5/" rel="prev" title="2020年04月20日软件设计师每日一练">
      <i class="fa fa-chevron-left"></i> 2020年04月20日软件设计师每日一练
    </a></div>
      <div class="post-nav-item">
    <a href="/blog/2020/04/21/2020%E5%B9%B404%E6%9C%8821%E6%97%A5/" rel="next" title="2020年04月21日软件设计师每日一练">
      2020年04月21日软件设计师每日一练 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  

    <!-- 引入share.js -->
    
      <div data-weibo-title="分享到微博" data-qq-title="分享到QQ" data-douban-title="分享到豆瓣" class="social-share" class="share-component" data-disabled="twitter,facebook" data-description="Share.js - 一键分享到微博，QQ空间，腾讯微博，人人，豆瓣">分享到：</div>
    
  </div>


          </div>
          
    
  <div class="comments">
    <div id="lv-container" data-id="city" data-uid="MTAyMC80OTExNy8yNTYxMg=="></div>
  </div>
  

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#前言"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#1-重点概念"><span class="nav-number">2.</span> <span class="nav-text">1 重点概念</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-1-结点概念"><span class="nav-number">2.1.</span> <span class="nav-text">1.1 结点概念</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#1-2-树结点声明"><span class="nav-number">2.2.</span> <span class="nav-text">1.2 树结点声明</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#2-树"><span class="nav-number">3.</span> <span class="nav-text">2 树</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-定义"><span class="nav-number">3.1.</span> <span class="nav-text">2.1 定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-结点的度"><span class="nav-number">3.2.</span> <span class="nav-text">2.2 结点的度</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#2-3-结点关系"><span class="nav-number">3.2.1.</span> <span class="nav-text">2.3 结点关系</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-4-结点层次"><span class="nav-number">3.2.2.</span> <span class="nav-text">2.4 结点层次</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#2-5-树的深度"><span class="nav-number">3.2.3.</span> <span class="nav-text">2.5 树的深度</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#3-二叉树"><span class="nav-number">4.</span> <span class="nav-text">3 二叉树</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#3-1-定义"><span class="nav-number">4.1.</span> <span class="nav-text">3.1 定义</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-2-二叉树特点"><span class="nav-number">4.2.</span> <span class="nav-text">3.2 二叉树特点</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-3-二叉树性质"><span class="nav-number">4.3.</span> <span class="nav-text">3.3 二叉树性质</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-4-斜树"><span class="nav-number">4.4.</span> <span class="nav-text">3.4 斜树</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-5-满二叉树"><span class="nav-number">4.5.</span> <span class="nav-text">3.5 满二叉树</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-6-完全二叉树"><span class="nav-number">4.6.</span> <span class="nav-text">3.6 完全二叉树</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-7-二叉树的存储结构"><span class="nav-number">4.7.</span> <span class="nav-text">3.7 二叉树的存储结构</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-7-1-顺序存储"><span class="nav-number">4.7.1.</span> <span class="nav-text">3.7.1 顺序存储</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-7-2-二叉链表"><span class="nav-number">4.7.2.</span> <span class="nav-text">3.7.2 二叉链表</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3-8-二叉树遍历"><span class="nav-number">4.8.</span> <span class="nav-text">3.8 二叉树遍历</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#3-8-1-定义"><span class="nav-number">4.8.1.</span> <span class="nav-text">3.8.1 定义</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-8-2-前序遍历"><span class="nav-number">4.8.2.</span> <span class="nav-text">3.8.2 前序遍历</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-8-3-中序遍历"><span class="nav-number">4.8.3.</span> <span class="nav-text">3.8.3 中序遍历</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-8-4-后序遍历"><span class="nav-number">4.8.4.</span> <span class="nav-text">3.8.4 后序遍历</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-8-5-层次遍历"><span class="nav-number">4.8.5.</span> <span class="nav-text">3.8.5 层次遍历</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#3-8-6-遍历常考考点"><span class="nav-number">4.8.6.</span> <span class="nav-text">3.8.6 遍历常考考点</span></a></li></ol></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#4-结语"><span class="nav-number">5.</span> <span class="nav-text">4 结语</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#大部分引用"><span class="nav-number">6.</span> <span class="nav-text">大部分引用</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Chenyumeng"
      src="/blog/images/touxiang.JPG">
  <p class="site-author-name" itemprop="name">Chenyumeng</p>
  <div class="site-description" itemprop="description">用来记录自己学习中所遇到的问题以及如何解决和自己所学知识的理解</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/blog/archives/">
        
          <span class="site-state-item-count">32</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/blog/categories/">
          
        <span class="site-state-item-count">1</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/blog/tags/">
          
        <span class="site-state-item-count">29</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="https://github.com/chen-yumeng" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;chen-yumeng" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://weibo.com/u/3123221441" title="Weibo → https:&#x2F;&#x2F;weibo.com&#x2F;u&#x2F;3123221441" rel="noopener" target="_blank"><i class="fa fa-fw fa-weibo"></i>Weibo</a>
      </span>
  </div>


  <div class="links-of-blogroll motion-element">
    <div class="links-of-blogroll-title">
      <i class="fa fa-fw fa-link"></i>
      Links
    </div>
    <ul class="links-of-blogroll-list">
        <li class="links-of-blogroll-item">
          <a href="https://github.com/" title="https:&#x2F;&#x2F;github.com&#x2F;" rel="noopener" target="_blank">Github</a>
        </li>
    </ul>
  </div>

      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Chenyumeng</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-area-chart"></i>
    </span>
    <span title="站点总字数">390k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">5:55</span>
</div>

        






  <script>
  function leancloudSelector(url) {
    url = encodeURI(url);
    return document.getElementById(url).querySelector('.leancloud-visitors-count');
  }
  if (CONFIG.page.isPost) {
    function addCount(Counter) {
      var visitors = document.querySelector('.leancloud_visitors');
      var url = decodeURI(visitors.id);
      var title = visitors.dataset.flagTitle;

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url })))
        .then(response => response.json())
        .then(({ results }) => {
          if (results.length > 0) {
            var counter = results[0];
              leancloudSelector(url).innerText = counter.time + 1;
            Counter('put', '/classes/Counter/' + counter.objectId, { time: { '__op': 'Increment', 'amount': 1 } })
              .then(response => response.json())
              .catch(error => {
                console.error('Failed to save visitor count', error);
              })
          } else {
              Counter('post', '/classes/Counter', { title, url, time: 1 })
                .then(response => response.json())
                .then(() => {
                  leancloudSelector(url).innerText = 1;
                })
                .catch(error => {
                  console.error('Failed to create', error);
                });
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }
  } else {
    function showTime(Counter) {
      var visitors = document.querySelectorAll('.leancloud_visitors');
      var entries = [...visitors].map(element => {
        return decodeURI(element.id);
      });

      Counter('get', '/classes/Counter?where=' + encodeURIComponent(JSON.stringify({ url: { '$in': entries } })))
        .then(response => response.json())
        .then(({ results }) => {
          for (let url of entries) {
            let target = results.find(item => item.url === url);
            leancloudSelector(url).innerText = target ? target.time : 0;
          }
        })
        .catch(error => {
          console.error('LeanCloud Counter Error', error);
        });
    }
  }

  fetch('https://app-router.leancloud.cn/2/route?appId=')
    .then(response => response.json())
    .then(({ api_server }) => {
      var Counter = (method, url, data) => {
        return fetch(`https://${api_server}/1.1${url}`, {
          method,
          headers: {
            'X-LC-Id'     : '',
            'X-LC-Key'    : '',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify(data)
        });
      };
      if (CONFIG.page.isPost) {
        if (CONFIG.hostname !== location.hostname) return;
        addCount(Counter);
      } else if (document.querySelectorAll('.post-title-link').length >= 1) {
        showTime(Counter);
      }
    });
  </script>


      </div>
    </footer>
  </div>

  
  <script src="/blog/lib/anime.min.js"></script>
  <script src="/blog/lib/velocity/velocity.min.js"></script>
  <script src="/blog/lib/velocity/velocity.ui.min.js"></script>

<script src="/blog/js/utils.js"></script>

<script src="/blog/js/motion.js"></script>


<script src="/blog/js/schemes/pisces.js"></script>


<script src="/blog/js/next-boot.js"></script>




  




  
<script src="/blog/js/local-search.js"></script>













  

  

<script>
NexT.utils.loadComments(document.querySelector('#lv-container'), () => {
  window.livereOptions = {
    refer: location.pathname.replace(CONFIG.root, '').replace('index.html', '')
  };
  (function(d, s) {
    var j, e = d.getElementsByTagName(s)[0];
    if (typeof LivereTower === 'function') { return; }
    j = d.createElement(s);
    j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
    j.async = true;
    e.parentNode.insertBefore(j, e);
  })(document, 'script');
});
</script>

  <script src="/dist/js/social-share.min.js"></script>
</body>
</html>
